#!/usr/bin/env python
# -*- coding:utf-8 -*-
import redis
import requests
from bs4 import BeautifulSoup
import time
import json

session = requests.session()
host = 'https://www.1399p.com'
url = host + '/pk10/gyhomit?page=1&ball=0&type=0&sort=gyh&order=desc&_=' + str(time.time())
url2 = host + '/pk10/getawarddata?r=' + str(time.time())

try:
    # get wardresult:
    resp2 = session.get(url2)
    r = redis.Redis(host='127.0.0.1', port=6379, db=0, password='')
    curinfo = r.get('pk10_curinfo')
    if curinfo is None:
        r.set('pk10_curinfo', resp2.text)
    else:
        dict_sum = json.loads(curinfo)
        haswardno = int(dict_sum.get('current').get('period'))  # now wardnums
        dict_sum2 = json.loads(resp2.text)
        currentno = int(dict_sum2.get('current').get('period'))  # current wardnums
        if currentno > haswardno:
            r.set('pk10_curinfo', resp2.text)

    # get hezhiresult:
    resp = session.get(url)
    soup = BeautifulSoup(resp.text, 'html.parser')
    trs = soup.find_all('tr', attrs={'class': {"", "bg_gray"}})
    for tr in trs:
        tds = tr.find_all(name='td')
        hstr = str(tds[2].string.strip())
        todaystr = str(tds[1].string.strip())
        maxval = str(tds[6].string.strip())
        desnum = 'pk10_num_' + str(tds[0].string)
        maxnum = 'pk10_maxnum_' + str(tds[0].string)
        todaynum = 'pk10_today_' + str(tds[0].string)
        r.set(desnum, hstr)
        r.set(maxnum, maxval)
        r.set(todaynum, todaystr)
except Exception as e:
    pass
